﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace TradeTestWeb
{
    public partial class ecs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            
        }

        protected void btncal_Click(object sender, EventArgs e)
        {
            var xml = new XmlDocument();
            xml.Load(FileUpload1.FileContent);
            var sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("");
            sb.AppendLine("namespace SWSoft.Framework");
            sb.AppendLine("{");

            sb.AppendLine("\t/// <summary>");
            sb.AppendLine("\t/// SQL语句配置集合");
            sb.AppendLine("\t/// </summary>");
            sb.AppendLine("\tpublic class SQ");
            sb.AppendLine("\t{");
            foreach (XmlNode item in xml.LastChild.ChildNodes)
            {
                if (item.Name.ToUpper() == "SQI")
                {
                    sb.AppendLine("\t\t/// <summary>");
                    sb.AppendFormat("\t\t/// {0}\r\n", item.Attributes["tooltip"].Value);
                    sb.AppendLine("\t\t/// </summary>");
                    sb.AppendFormat("\t\tpublic const string {0}=\"{0}\";", item.Attributes["id"].Value);
                    sb.AppendLine("");
                }
                else if (item.Name.ToUpper() == "PROC")
                {
                    sb.AppendLine("\t\t/// <summary>");
                    sb.AppendFormat("\t\t/// 存储过程:{0}\r\n", item.Attributes["tooltip"].Value);
                    sb.AppendLine("\t\t/// </summary>");
                    sb.AppendFormat("\t\tpublic const string P_{0}=\"{0}\";", item.Attributes["id"].Value);
                    sb.AppendLine("");
                }
            }
            sb.AppendLine("\t}");
            sb.AppendLine("}");
            var file = Server.MapPath("~") + "\\SQ.cs";
            File.WriteAllText(file, sb.ToString(), Encoding.UTF8);
            Response.ClearHeaders();
            Response.BufferOutput = false;
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-disposition", "attachment;filename=SQ.cs");
            Response.TransmitFile(file);
            Response.Flush();
            Response.End();
        }

        protected void FileUpload1_Init(object sender, EventArgs e)
        {
        }
    }
}